package xuli.service.impl;

import entity.Order_Cart;
import xuli.dao.ShoppingCartDao;
import xuli.dao.impl.ShoppingCartDaoImpl;
import xuli.service.ShoppingCartService;

import java.util.List;

public class ShoppingCartServiceImpl implements ShoppingCartService {
    private ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();


    @Override
    public boolean insert(Order_Cart orderCart) {
        Order_Cart byUserIdAndProductIdAndModelId = shoppingCartDao.findByUserIdAndProductIdAndModelId(orderCart.getUser_id(), orderCart.getProduct_id(), orderCart.getModel_id());
        if (byUserIdAndProductIdAndModelId != null) {
            byUserIdAndProductIdAndModelId.setProduct_amount(byUserIdAndProductIdAndModelId.getProduct_amount() + orderCart.getProduct_amount());
            return shoppingCartDao.update(byUserIdAndProductIdAndModelId);
        } else {
            return shoppingCartDao.insert(orderCart);
        }
    }

    @Override
    public List<Order_Cart> findAll(Integer userId) {

        return shoppingCartDao.findAll(userId);

    }
}
